const Results = require("../../config/results")
const writeErrLog = require('../../log/writeErrLog.js')
const {addErrLogToMySQL} = require("../../log/addErrLogToMySQL.js");
module.exports = error = (err, req, res, next) => {
    addErrLogToMySQL({name: err.name || null, code: err.code || null, message: err.message || null})
    // writeErrLog(undefined, err.name || '', err.code || '', err.message || '')
    if (err.name === 'UnauthorizedError') {
        // 这次错误是由 token 解析失败导致的
        return res.status(401).json(
            Results.fail(401, err.message || '', err.code || [])
        )
    }
    res.status(500).json(Results.fail(500, err.message || '', err.code || []))
}
